import { createRouter, createWebHistory} from 'vue-router'
// @ts-ignore
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// @ts-ignore
import  useUserStore  from '../store/modules/user'

// 实例化路由实例
const router = createRouter({
    history: createWebHistory(),
    // 配置路由
    routes: [
        {
            path: '/home',
            name: 'home',
            //@ts-ignore
            component: () => import('@/pages/home/index.vue') // 懒加载组件，只在需要时加载
        },
        {
            path: '/hospital',
            name: 'hospital',
            //@ts-ignore
            component: () => import('@/pages/hospital/index.vue'), // 懒加载组件，只在需要时加载
            children: [
                {
                    path: 'register',
                    //@ts-expect-error
                    component: () => import('@/pages/hospital/register/index.vue')
                },
                {
                    path: 'detail',
                    //@ts-expect-error

                    component: () => import('@/pages/hospital/detail/index.vue')
                },
                {
                    path: 'info',
                    //@ts-expect-error

                    component: () => import('@/pages/hospital/info/index.vue')
                },
                {
                    path: 'notice',
                    //@ts-expect-error

                    component: () => import('@/pages/hospital/notice/index.vue')
                },
                {
                    path: 'search',
                    //@ts-expect-error

                    component: () => import('@/pages/hospital/search/index.vue')
                },
                {
                    path: 'register/detail',
                    //@ts-expect-error

                    component: () => import('@/pages/hospital/register/detail.vue')

                },
                {
                    path: 'register/interface',
                    //@ts-expect-error

                    component: () => import('@/pages/hospital/register/interface.vue')
                }
            ]
        },
        {
            path: '/',
            redirect: '/home' // 默认路由重定向到home页面
        },
        {
            path: '/user',
            name: 'user',
            //@ts-ignore
            component: () => import('@/pages/user/index.vue'),
            children: [
                {
                    path: 'vip',
                    name: 'Vip',
                    //@ts-ignore
                    component: () => import('@/pages/user/certifiction/index.vue')
                },
                {
                    path: 'order',
                    name: 'order',
                    //@ts-ignore
                    component: () => import('@/pages/user/order/index.vue')
                },
                {
                    path: 'patient',
                    name: 'patient',
                    //@ts-ignore
                    component: () => import('@/pages/user/patient/index.vue')
                },
                {
                    path: 'profile',
                    name: 'profile',
                    //@ts-ignore
                    component: () => import('@/pages/user/profile/index.vue')
                },
                {
                    path: 'feedback',
                    name: 'feedback',
                    //@ts-ignore
                    component: () => import('@/pages/user/feedback/index.vue')
                },
                {
                    path: 'order-detail',
                    name:'detail',
                    //@ts-ignore
                    component: () => import('@/pages/user/order/detail.vue')
                },
                {
                    path: 'patient-add',
                    name:'add',
                    //@ts-ignore
                    component: () => import('@/pages/user/patient/add.vue')
                }
            ]
        }
    ],
    // 滚动行为： 控制滚动条位置
    scrollBehavior() {
        return {
            top: 0, // 滚动到页面顶部,
            left: 0 // 滚动到页面左侧
        }
    }
})

// 未登录可访问的路由
const whiteList = ['/home', '/hospital/register','/hospital/detail',,'/hospital/info', '/hospital/notice', '/hospital/search']

// 全局前置守卫
router.beforeEach((to, from, next) =>  {
    Nprogress.start()
    if(whiteList.includes(to.path)) {
        next()
    } else{
        const userStore = useUserStore()
        if(userStore.token) {
            next()
        } else {
            userStore.setDialogShow(true)
        next(from.fullPath)

        }
    }
    
})

// 全局后置钩子
router.afterEach(() => {
    Nprogress.done()
})
export default router

